package com.content.Service.Impl.show;

import com.common.Utils.JwtUtil;
import com.content.Entity.show.Show;
import com.content.Mapper.show.ShowMapper;
import com.content.Service.show.ShowService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;
import java.util.Random;

@Service
public class ShowServiceImpl implements ShowService {
    @Autowired
    private ShowMapper showMapper;
    @Override
    public PageInfo<Show> getShowList(Integer pageNum, Integer pageSize, String showName) {
        PageHelper.startPage(pageNum,pageSize);
        List<Show> showList = showMapper.getShowList(showName);
        return new PageInfo<>(showList);
    }

    @Override
    public void addShow(Show show) {
        String token = show.getToken();
        String username = JwtUtil.extractUsername(token);

        show.setShowId(makeShowId());
        show.setCreateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        show.setCreateUser(username);


        showMapper.addShow(show);

    }

    @Override
    public void deleteShow(String showId) {

        showMapper.deleteShow(showId);
        showMapper.deleteShowProgram(showId);

    }

    @Override
    public void updateShow(Show show) {
        String token = show.getToken();
        String username = JwtUtil.extractUsername(token);
        show.setUpdateUser(username);
        show.setUpdateTime(LocalDateTime.now(ZoneId.of("Asia/Shanghai")));
        showMapper.updateShow(show);
    }

    @Override
    public List<Show> getShows() {
        return showMapper.getShows();
    }

    @Override
    public Show getShowById(String showId) {
       return showMapper.getShowById(showId);

    }


    public  String makeShowId(){
        long timestamp = System.currentTimeMillis();
        String random = String.format("%04d", new Random().nextInt(1000));
        return "S" + timestamp + random;
    }

}
